package servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.catalina.Session;
import service.CheckUserPwdByRole;
import util.GetRealIp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//@Deprecated
@WebServlet(name = "登录校验",value = "/loginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String json="";
		ObjectMapper mapper = new ObjectMapper();
		String userName=request.getParameter("username");
		String passWord=request.getParameter("password");
		String role=request.getParameter("role");
		int flag= CheckUserPwdByRole.check(userName,passWord,role);
		if(userName.length()==11){userName=CheckUserPwdByRole.changeTelToId(userName,role);}
		HttpSession session = request.getSession();
		session.setAttribute("userName",userName);
		session.setAttribute("passWord",passWord);
		session.setAttribute("role",role);
		session.setAttribute("flag","true");
		System.out.println(flag);
		switch (flag){
			case 1:
				//跳转到管理员权限的医生页面
				json="{\"flag\":\"1\",\"msg\":\"管理员登录成功\"}";
				mapper.writeValue(response.getWriter(), json);
			case 2:
				//跳转到医生页面
				json="{\"flag\":\"2\",\"msg\":\"医生登录成功\"}";
				mapper.writeValue(response.getWriter(), json);
			case 3:
				//跳转家属页面
				json="{\"flag\":\"3\",\"msg\":\"家属登录成功\"}";
				mapper.writeValue(response.getWriter(), json);
			case 4:
				//跳转家属页面
				json="{\"flag\":\"4\",\"msg\":\"游客登录成功\"}";
				mapper.writeValue(response.getWriter(), json);
			case -1:
				//
				json="{\"flag\":\"-1\",\"msg\":\"密码错误\"}";
				mapper.writeValue(response.getWriter(), json);
			case -2:
				json="{\"flag\":\"-2\",\"msg\":\"无用户\"}";
				mapper.writeValue(response.getWriter(), json);
			case -3:
				mapper.writeValue(response.getWriter(), json);
		}


	}

}